package com.bos.web.action;

import com.alibaba.fastjson.JSON;
import com.bos.domian.base.Standard;
import com.bos.service.StandardService;
import com.bos.web.BaseAction;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.net.httpserver.Authenticator;
import org.apache.struts2.convention.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月11号:21点07分
 * @mailbox mynameisweiyan@gmail.com
 */
//需要进行异步操作,所以返回值也是json类型的,  json-default是集成struts-default,所以不需要考虑拦截器链加载问题
@ParentPackage("json-default")
@Actions
@Namespace("/")
@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)//多例的
    public class StandardAction extends BaseAction<Standard> {
    @Autowired
    private StandardService standardService;

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    private Integer cid;

    //保存操作
    @Action(value = "standard_save", results = {@Result(name = "SUCCESS", location = "/pages/base/standard.html")})
    public String standareSave() {
        model.setOperatingTime(new Date());
        standardService.save(model);
        return "SUCCESS";

    }

    //用来收派员的取货标准作为统计。
    @Action(value = "standard_findAll", results = {@Result(type = "json")})
    public String standardFindAll() {
        List<Standard> standards = standardService.findAll();
        ActionContext.getContext().getValueStack().push(standards);
        return SUCCESS;
    }

    //单一查询操作
    @Action(value = "standard_findOne")
    public Standard standardFindOne() {
        return standardService.findOne(cid);
    }

    //执行更新数据操作
    @Action(value = "standard_update")
    public boolean standardUpdate() {

        try {
            standardService.update(model);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private List<Integer> ids;

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
//执行删除操作

    @Action(value = "standard_delete", results = {@Result(name = "success", type = "json")})
    public String standardDelete() {
        try {
            standardService.delete(ids);
            ActionContext.getContext().getValueStack().push(true);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            ActionContext.getContext().getValueStack().push(false);
            return "success";
        }

    }


    @Action(value = "standard_pageQuery", results = {@Result(name = "success", type = "json")})
    public String standardPageQuery() {

        //存入值栈                                                  //分页都是从第0页开始的!
        ActionContext.getContext().getValueStack().push(standardService.standardPageQuery(page - 1, rows));
        return "success";
    }
}
